# -*- coding:utf-8 -*-
# !/usr/bin/python3
import core.data_handler as hj212
import util.fun_log as log
import socket_utils.tcp_client as tcp_client
import time

ip = '127.0.0.1'
port = 9999

MN = '130481LBYMH001'
# 130481LBYMH001
# 130481BYJH0006
# 130481BYJH0005
# 130481BYMH0002

ST = '59'
PW = '123456'
Flag = '5'


def send_rtd():
    DIC_HJ212_2017 = {}
    # DIC_HJ212_2017['QN']=hj212.get_time_stamp()
    DIC_HJ212_2017['ST'] = ST
    DIC_HJ212_2017['CN'] = 2011
    DIC_HJ212_2017['PW'] = PW
    DIC_HJ212_2017['MN'] = MN
    # DIC_HJ212_2017['Flag']=Flag
    DIC_HJ212_2017['CP'] = {}

    DIC_HJ212_2017['CP'][0] = {}
    DIC_HJ212_2017['CP'][0]['DataTime'] = hj212.get_time_stamp()[0:14]

    DIC_HJ212_2017['CP'][1] = {}
    DIC_HJ212_2017['CP'][1]['SCXBH-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][2] = {}
    DIC_HJ212_2017['CP'][2]['JHGYDZMSJ-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][3] = {}
    DIC_HJ212_2017['CP'][3]['JHGYDTJSJ-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][4] = {}
    DIC_HJ212_2017['CP'][4]['JHGYDZMCDL-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][5] = {}
    DIC_HJ212_2017['CP'][5]['JHGYDTJCDL-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][6] = {}
    DIC_HJ212_2017['CP'][6]['JHGYDMQSYL-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][7] = {}
    DIC_HJ212_2017['CP'][7]['CCSSBDCCQQHYC-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][8] = {}
    DIC_HJ212_2017['CP'][8]['CCSSDCCQRCDL-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][9] = {}
    DIC_HJ212_2017['CP'][9]['CCSSDCCQRCDY-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][10] = {}
    DIC_HJ212_2017['CP'][10]['TLSSXSJPHZ-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][11] = {}
    DIC_HJ212_2017['CP'][11]['TLSSGJLL-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][12] = {}
    DIC_HJ212_2017['CP'][12]['TLSSGLLL-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][13] = {}
    DIC_HJ212_2017['CP'][13]['TLSSJYXHBYXZT-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][14] = {}
    DIC_HJ212_2017['CP'][14]['TLSSSFYHFJDL-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][15] = {}
    DIC_HJ212_2017['CP'][15]['TXSSYQSD-Rtd'] = 10.01

    DIC_HJ212_2017['CP'][16] = {}
    DIC_HJ212_2017['CP'][16]['TXSSTXHYJYL-Rtd'] = 10.01

    tcp_client.send(ip, port, hj212.encode(DIC_HJ212_2017))
    log.write(hj212.encode(DIC_HJ212_2017))


def send_his(CN):
    DIC_HJ212_2017 = {}
    DIC_HJ212_2017['QN'] = hj212.get_time_stamp()
    DIC_HJ212_2017['ST'] = ST
    DIC_HJ212_2017['CN'] = CN
    DIC_HJ212_2017['PW'] = PW
    DIC_HJ212_2017['MN'] = MN
    DIC_HJ212_2017['Flag'] = Flag

    DIC_HJ212_2017['CP'] = {}

    DIC_HJ212_2017['CP'][0] = {}
    DIC_HJ212_2017['CP'][0]['DataTime'] = DIC_HJ212_2017['QN'][0:14]

    DIC_HJ212_2017['CP'][1] = {}  # PH值
    DIC_HJ212_2017['CP'][1]['w01001-Min'] = 6.9394
    DIC_HJ212_2017['CP'][1]['w01001-Avg'] = 6.9394
    DIC_HJ212_2017['CP'][1]['w01001-Max'] = 6.9394
    DIC_HJ212_2017['CP'][1]['w01001-Flag'] = 'N'

    DIC_HJ212_2017['CP'][2] = {}  # 化学需氧量
    DIC_HJ212_2017['CP'][2]['w01018-Cou'] = 72.4002
    DIC_HJ212_2017['CP'][2]['w01018-Min'] = 72.4002
    DIC_HJ212_2017['CP'][2]['w01018-Avg'] = 72.4002
    DIC_HJ212_2017['CP'][2]['w01018-Max'] = 72.4002
    DIC_HJ212_2017['CP'][2]['w01018-Flag'] = 'N'

    DIC_HJ212_2017['CP'][3] = {}  # 氨氮
    DIC_HJ212_2017['CP'][3]['w21003-Cou'] = 2.5283
    DIC_HJ212_2017['CP'][3]['w21003-Min'] = 2.5283
    DIC_HJ212_2017['CP'][3]['w21003-Avg'] = 2.5283
    DIC_HJ212_2017['CP'][3]['w21003-Max'] = 2.5283
    DIC_HJ212_2017['CP'][3]['w21003-Flag'] = 'N'

    DIC_HJ212_2017['CP'][4] = {}  # 氰化物
    DIC_HJ212_2017['CP'][4]['w21016-Cou'] = 0.0367
    DIC_HJ212_2017['CP'][4]['w21016-Min'] = 0.0367
    DIC_HJ212_2017['CP'][4]['w21016-Avg'] = 0.0367
    DIC_HJ212_2017['CP'][4]['w21016-Max'] = 0.0367
    DIC_HJ212_2017['CP'][4]['w21016-Flag'] = 'N'

    DIC_HJ212_2017['CP'][5] = {}  # 污水
    DIC_HJ212_2017['CP'][5]['w00000-Cou'] = 29.2936
    DIC_HJ212_2017['CP'][5]['w00000-Min'] = 29.2936
    DIC_HJ212_2017['CP'][5]['w00000-Avg'] = 29.2936
    DIC_HJ212_2017['CP'][5]['w00000-Max'] = 29.2936
    DIC_HJ212_2017['CP'][5]['w00000-Flag'] = 'N'

    DIC_HJ212_2017['CP'][6] = {}  # 挥发酚
    DIC_HJ212_2017['CP'][6]['w23002-Cou'] = 0.0401
    DIC_HJ212_2017['CP'][6]['w23002-Min'] = 0.0401
    DIC_HJ212_2017['CP'][6]['w23002-Avg'] = 0.0401
    DIC_HJ212_2017['CP'][6]['w23002-Max'] = 0.0401
    DIC_HJ212_2017['CP'][6]['w23002-Flag'] = 'N'

    tcp_client.send(ip, port, hj212.encode(DIC_HJ212_2017))
    log.write(hj212.encode(DIC_HJ212_2017))


def avg(data):
    return sum(data) / len(data)


def main():
    send_rtd()
    log.write('发送完成')
    return

    send_his(2051)

    while True:
        timestamp = time.time()
        local_time = time.localtime(timestamp)
        if local_time.tm_sec == 55:  # 每分钟
            send_rtd()
            send_his(2051)
            log.write('发送分钟值')
        if local_time.tm_sec == 55 and local_time.tm_min == 59:  # 每小时
            send_his(2061)
            log.write('发送小时值')
        if local_time.tm_sec == 55 and local_time.tm_min == 59 and local_time.tm_hour == 23:  # 每天
            send_his(2031)
            log.write('发送日值')
        time.sleep(1)
    # 自我测试


if __name__ == "__main__":
    main()
